<html>
<head>
<link rel='stylesheet' href='../../../js/sh/SyntaxHighlighter.css' type='text/css' />
<script src='../../../js/sh/shCore.js'></script>
<script src='../../../js/sh/shBrushJava.js'></script>
<style>
* {
font-family:Courier New,monospace;
  padding: 0;
  margin: 0;
  white-space: nowrap;
  font-size: 11px;
}
.dp-highlighter {
  white-space: nowrap;
  overflow: visible;
  width: 600px;
  font-size: 11px;
  font-family:Courier New,monospace;
}
</style>
</head>
<body>
<textarea name='code' class='java:nogutter' rows='15' cols='120'>
/*
 * SmartGWT (GWT for SmartClient)
 * Copyright 2008 and beyond, Isomorphic Software, Inc.
 *
 * SmartGWT is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation.  SmartGWT is also
 * available under typical commercial license terms - see
 * http://smartclient.com/license
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 */

import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.SchemaSet;
import com.smartgwt.client.data.XMLTools;
import com.smartgwt.client.data.XSDLoadCallback;
import com.smartgwt.client.data.fields.DataSourceDateField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.sample.showcase.client.data.ItemRecord;

public class XsdDataSourceSample implements EntryPoint {

    public void onModuleLoad() {

        final ListGrid listGrid = new ListGrid();
        listGrid.setCanEdit(true);

        final DynamicForm dynamicForm = new DynamicForm();

        final IButton validateButton = new IButton("Validate");
        validateButton.setDisabled(true);

        validateButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                dynamicForm.validate();

            }
        });

        IButton loadButton = new IButton("Load Schema");
        loadButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                XMLTools.loadXMLSchema("data/dataIntegration/xml/supplyItem.xsd", new XSDLoadCallback() {
                    public void execute(SchemaSet schemaSet) {
                        loadXMLSchemaReply(schemaSet, listGrid, dynamicForm, validateButton);
                    }
                });
            }
        });

        VLayout layout = new VLayout(15);
        layout.addMember(loadButton);
        layout.addMember(listGrid);
        layout.addMember(dynamicForm);
        layout.addMember(validateButton);

        layout.draw();
    }

    private void loadXMLSchemaReply(SchemaSet schemaSet, ListGrid listGrid, DynamicForm dynamicForm, IButton validateButton) {
        DataSource schemaDS = schemaSet.getSchema("supplyItem");

        DataSource dataSource = new DataSource();
        dataSource.setInheritsFrom(schemaDS);
        dataSource.setUseParentFieldOrder(true);

        DataSourceTextField itemID = new DataSourceTextField("itemId");
        itemID.setHidden(true);
        itemID.setPrimaryKey(true);

        DataSourceTextField itemName = new DataSourceTextField("itemName", "item name");
        DataSourceDateField nextShipment = new DataSourceDateField("nextShipment", "next shipment");
        nextShipment.setUseTextField(true);

        dataSource.setFields(itemID, itemName, nextShipment);

        listGrid.setDataSource(dataSource);
        dynamicForm.setDataSource(dataSource);

        // to demonstrate grid editors, add sample data and start editing

        listGrid.setData(new ItemRecord[]{new ItemRecord(123, "Sample Item", "1091600", null, "Rollfix Glue", "Ea", 3.73, null, null)});
        listGrid.startEditing();

        // to demonstrate validation, use values that will fail validation
        dynamicForm.setValue("unitCost", -1.234);
        dynamicForm.setValue("SKU", "thisSkuIsWayTooLong");
        validateButton.enable();

    }

}
</textarea>
<script class='javascript'>
dp.SyntaxHighlighter.HighlightAll("code");
</script>
</body>
</html>
